package com.qububu.db.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qububu.db.dao.DineDao;
import com.qububu.play.dineTogether.dto.ActionInfoDto;
import com.qububu.play.dineTogether.dto.ActionPeopleDto;
import com.qububu.play.dineTogether.dto.PositionDto;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemUtil;
/**
 * @功能 与系统公告有关的数据库操作
 * */
@Repository
public class DineDaoImpl implements DineDao {

	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}


	/**
	 * @功能 添加活动
	 */
	public void doAddActionApply(ActionInfoDto actionInfoDto) {
		String sql = "insert into action_info(action_title, action_pic_url, content, use_money, begin_time, end_time, publish_time, people_num, author_name, position_id ) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
		Object[] addActionInfo = {
			actionInfoDto.getActionTitle(),
			actionInfoDto.getActionPicUrl(),
			actionInfoDto.getContent(),
			actionInfoDto.getUseMoney(),
			actionInfoDto.getBeginTime(),
			actionInfoDto.getEndTime(),
			actionInfoDto.getPublishTime(),
			actionInfoDto.getPeopleNum(),
			actionInfoDto.getAuthorName(),
			ParamConverter.convertToLongObj(SystemUtil.decrypt(actionInfoDto.getPosition().getPositionId() ) )
		};	
		jdbcTemplate.update(sql, addActionInfo);
		
	}

	public void doModifyActionInfoById(ActionInfoDto actionInfoDto) {
		String sql = "update action_info set action_title = ?, action_pic_url = ?, content = ?, use_money = ?, begin_time = ?, end_time = ?, people_num = ?, author_name = ? where action_id = ?";

		Object[] modifyActionInfo = {
				actionInfoDto.getActionTitle(),
				actionInfoDto.getActionPicUrl(),
				actionInfoDto.getContent(),
				actionInfoDto.getUseMoney(),
				actionInfoDto.getBeginTime(),
				actionInfoDto.getEndTime(),
				actionInfoDto.getPeopleNum(),
				actionInfoDto.getAuthorName(),
				actionInfoDto.getActionId()
			};

		jdbcTemplate.update(sql, modifyActionInfo);
	}

	public List<ActionInfoDto> getActionInfoList() {
		
		String sql = "SELECT * FROM action_info ORDER BY end_time DESC";
		List<ActionInfoDto> actionList = null;
		actionList = new ArrayList<ActionInfoDto>();
		
		List<Map<String, Object>> actionMap = jdbcTemplate.queryForList(sql);
		ActionInfoDto actionInfoDto = null;
		for (Map<String, Object> elem : actionMap) {
			PositionDto positionDto = new PositionDto( (Long)elem.get("position_id"));
			positionDto.setPositionName(String.valueOf(elem.get("position_name")));
			
			actionInfoDto = new ActionInfoDto( (Long)elem.get("action_id"));
			actionInfoDto.setActionTitle(String.valueOf(elem.get("action_title")));
			actionInfoDto.setActionPicUrl(String.valueOf(elem.get("action_pic_url")));
			actionInfoDto.setAuthorName(String.valueOf(elem.get("author_name")));
			actionInfoDto.setContent(String.valueOf(elem.get("content")));
			actionInfoDto.setPeopleNum(ParamConverter.convertToInteger(String.valueOf(elem.get("people_num"))));
			actionInfoDto.setBeginTime(ParamConverter.convertToInteger(String.valueOf(elem.get("begin_time"))));
			actionInfoDto.setEndTime(ParamConverter.convertToInteger(String.valueOf(elem.get("end_time"))));
			actionInfoDto.setPublishTime(ParamConverter.convertToInteger(String.valueOf(elem.get("publish_time"))));
			actionInfoDto.setPosition(positionDto);
			
			actionList.add(actionInfoDto);
		}
		
		return actionList;
	}

	public ActionInfoDto getOngoingOrNearestInfo(int nowTime) {
		String sql = "SELECT * FROM action_info a, position p WHERE a.end_time > ? AND a.position_id = p.position_id ORDER BY a.end_time DESC LIMIT 0,1";
		
		List<Map<String, Object>> actionList = jdbcTemplate.queryForList(sql, nowTime);
		
		ActionInfoDto actionInfoDto = null;
		if (actionList.size() > 0) {
			Map<String, Object> actionMap = actionList.get(0);
			PositionDto positionDto = new PositionDto( (Long)actionMap.get("position_id"));
			positionDto.setPositionName(String.valueOf(actionMap.get("position_name")));
			
			actionInfoDto = new ActionInfoDto( (Long)actionMap.get("action_id"));
			actionInfoDto.setActionTitle(String.valueOf(actionMap.get("action_title")));
			actionInfoDto.setActionPicUrl(String.valueOf(actionMap.get("action_pic_url")));
			actionInfoDto.setAuthorName(String.valueOf(actionMap.get("author_name")));
			actionInfoDto.setContent(String.valueOf(actionMap.get("content")));
			actionInfoDto.setPeopleNum(ParamConverter.convertToInteger(String.valueOf(actionMap.get("people_num"))));
			actionInfoDto.setBeginTime(ParamConverter.convertToInteger(String.valueOf(actionMap.get("begin_time"))));
			actionInfoDto.setEndTime(ParamConverter.convertToInteger(String.valueOf(actionMap.get("end_time"))));
			actionInfoDto.setPublishTime(ParamConverter.convertToInteger(String.valueOf(actionMap.get("publish_time"))));
			actionInfoDto.setPosition(positionDto);
		}
		
		return actionInfoDto;
	}



	/**
	 * @备注 下边接连三个和上边功能一样，只不过分开实现
	 * 
	 **************************----Begin-----***************************/

	public List<ActionPeopleDto> getActionPeopleListByActionId(int actionIdInt) {
		List<ActionPeopleDto> peopleList = null;
		peopleList = new ArrayList<ActionPeopleDto>();
		
		String sql = "select * from action_people where action_id = ?";
		List<Map<String, Object>> peopleMap = jdbcTemplate.queryForList(sql, actionIdInt);
		
		ActionPeopleDto actionPeopleDto = null;
		for (Map<String, Object> elem : peopleMap) {
			actionPeopleDto = new ActionPeopleDto((Long)elem.get("people_id"), (Long)elem.get("staff_id"), (Long)elem.get("action_id"));
			actionPeopleDto.setPeopleName(String.valueOf(elem.get("people_name")));
			peopleList.add(actionPeopleDto);
		}
		
		return peopleList;
	}
	/**************************----End-----***************************/
	
	/**
	 * @功能 添加参加活动的人
	 * @操作表 action_people
	 */
	public void doAddActionPeople(ActionPeopleDto actionPeopleDto) {
		String addPeopleSql = "insert into action_people(people_id, people_name, staff_id, action_id) values(?, ?, ?, ?)";
		
		String peopleId = actionPeopleDto.getPeopleId();
//		peopleId = SystemUtil.decrypt(peopleId);
		String staffId = actionPeopleDto.getStaffId();
//		staffId = SystemUtil.decrypt(staffId);
		String actionId = actionPeopleDto.getActionId();
//		actionId = SystemUtil.decrypt(actionId);
		
		Object[] addPeopleVal = {
				peopleId, actionPeopleDto.getPeopleName(), staffId, actionId
		};
//		System.out.println("\n---------"+peopleId+"------"+actionPeopleDto.getPeopleName()+"---------"+staffId+"---------"+actionId+"------------------------\n\n");
		jdbcTemplate.update(addPeopleSql, addPeopleVal);
	}
}
